package com.leihub.xlfiletools.html.controller;

import com.leihub.xlfiletools.html.pojo.BatchProcess;
import com.leihub.xlfiletools.html.pojo.ConsolidatedTransactionData;
import com.leihub.xlfiletools.html.pojo.SystemMetricsData;
import com.leihub.xlfiletools.html.service.TableDisplayService;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;

import java.math.BigDecimal;
import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

/**
 * @author lei
 * @version 1.0
 * @description: TODO
 * @date 2024/8/3 6:31
 */
@Controller
public class TableController {



    private final TableDisplayService tableDisplayService;

    public TableController(TableDisplayService tableDisplayService) {
        this.tableDisplayService = tableDisplayService;
    }


    @GetMapping("/report")
    public String generateReport(Model model) {
        List<ConsolidatedTransactionData> transactionDataList = mockTransactionData();
        List<SystemMetricsData> systemMetricsDataList = mockSystemMetricsData();
        List<BatchProcess> batchProcessList = mockBatchProcesses();

        Map<String, List<List<String>>> horizontalDataMap = tableDisplayService.getHorizontalTableData(transactionDataList,"9375");
        Map<String, List<List<String>>> transactionTableDataMap =
                tableDisplayService.getVerticalTableData(transactionDataList, "tranCode", "分类",true);
        Map<String, List<List<String>>> systemMetricsTableDataMap =
                tableDisplayService.getVerticalTableData111(systemMetricsDataList, "大家撒大家阿是");
        Map<String, List<List<String>>> batchTableDataMap =
                tableDisplayService.getVerticalTableData(batchProcessList, "oichuli","分类",false);

        horizontalDataMap.forEach((key, value) -> {
            transactionTableDataMap.merge(key, value, (existingValue, newValue) -> {
                existingValue.addAll(newValue); // 将新数据添加到现有数据中
                return existingValue;
            });
        });

        // 合并数据
        Map<String, Map<String, List<List<String>>>> tableDataMap = new LinkedHashMap<>();
        tableDataMap.put("联机交易", transactionTableDataMap);
        tableDataMap.put("系统交易表", systemMetricsTableDataMap);
        tableDataMap.put("批处理", batchTableDataMap);

        model.addAttribute("date", "8923-32232");
        model.addAttribute("tableDataMap", tableDataMap);

        return "template"; // 返回模板文件名
    }

    private List<ConsolidatedTransactionData> mockTransactionData() {
        List<ConsolidatedTransactionData> transactionDataList = new ArrayList<>();

        transactionDataList.add(new ConsolidatedTransactionData(
                "Code1", "183059", "98.12%", "24.08ms",
                "186560", "100.0%", "23.62ms",
                "0", "0.0%", "0ms"
        ));

        transactionDataList.add(new ConsolidatedTransactionData(
                "Code2", "192345", "97.65%", "25.12ms",
                "200000", "100.0%", "22.58ms",
                "50", "0.25%", "30ms"
        ));

        transactionDataList.add(new ConsolidatedTransactionData(
                "Code3", "175000", "99.50%", "20.00ms",
                "175000", "100.0%", "21.45ms",
                "10", "0.05%", "25ms"
        ));

        transactionDataList.add(new ConsolidatedTransactionData(
                "Code4", "150000", "95.00%", "30.00ms",
                "157895", "100.0%", "24.85ms",
                "100", "0.50%", "35ms"
        ));

        transactionDataList.add(new ConsolidatedTransactionData(
                "Code5", "200000", "98.00%", "18.00ms",
                "204081", "100.0%", "20.35ms",
                "20", "0.10%", "22ms"
        ));

        return transactionDataList;
    }


    public List<SystemMetricsData> mockSystemMetricsData() {
        return List.of(
                new SystemMetricsData("Redis",
                        new BigDecimal("120.5"), new BigDecimal("100.3"),
                        new BigDecimal("85.7"), new BigDecimal("70.4"),
                        new BigDecimal("3056.2"), new BigDecimal("2980.3"),
                        new BigDecimal("500.0"), new BigDecimal("480.5")),
                new SystemMetricsData("Zookeeper",
                        new BigDecimal("110.1"), new BigDecimal("98.7"),
                        new BigDecimal("78.9"), new BigDecimal("65.3"),
                        new BigDecimal("2890.5"), new BigDecimal("2800.2"),
                        new BigDecimal("450.3"), new BigDecimal("420.7")),
                new SystemMetricsData("MySQL",
                        new BigDecimal("130.8"), new BigDecimal("120.6"),
                        new BigDecimal("95.4"), new BigDecimal("82.1"),
                        new BigDecimal("3100.8"), new BigDecimal("3050.5"),
                        new BigDecimal("550.2"), new BigDecimal("520.4"))
        );
    }


    private List<BatchProcess> mockBatchProcesses() {
        List<BatchProcess> batchProcessList = new ArrayList<>();

        // 详细的批处理数据模拟
        LocalDateTime preStartTime = LocalDateTime.of(2024, 8, 3, 8, 0); // 2024年8月3日08:00
        LocalDateTime preEndTime = LocalDateTime.of(2024, 8, 3, 9, 0);   // 2024年8月3日09:00

        LocalDateTime midStartTime = LocalDateTime.of(2024, 8, 3, 10, 0); // 2024年8月3日10:00
        LocalDateTime midEndTime = LocalDateTime.of(2024, 8, 3, 12, 0);   // 2024年8月3日12:00

        LocalDateTime postStartTime = LocalDateTime.of(2024, 8, 3, 13, 0); // 2024年8月3日13:00
        LocalDateTime postEndTime = LocalDateTime.of(2024, 8, 3, 14, 0);   // 2024年8月3日14:00

        BatchProcess batchProcess = new BatchProcess(preStartTime, preEndTime, midStartTime, midEndTime, postStartTime, postEndTime);
        batchProcessList.add(batchProcess);

        return batchProcessList;
    }
}
